//
//  rankingViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "rankingViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "SingerModel.h"
#import "rankListController.h"
#import "RankTableVIewCell.h"
#import "PendulumView.h"
@interface rankingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic ,retain)PendulumView *pendulum;
@end

@implementation rankingViewController
- (void)dealloc
{
    [_mytableview release];
    [_modelArray release];
    [super dealloc];
}

- (void)loadView{
    [super loadView];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self builtInterface];
    [self requestnet];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    self.pendulum = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:self.pendulum];
}

- (void)builtInterface
{
    self.mytableview = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain] autorelease];
    self.mytableview.delegate = self;
    self.mytableview.dataSource = self;
    [self.mytableview registerClass:[RankTableVIewCell class] forCellReuseIdentifier:@"cell"];
    self.mytableview.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.mytableview];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.modelArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankTableVIewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    SingerModel *model = [self.modelArray objectAtIndex:indexPath.row];
    NSString *strtitle = [NSString stringWithFormat:@"%@", model.title];
    NSDictionary *dic17 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect titlerect = [strtitle boundingRectWithSize:CGSizeMake(0, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic17 context:nil];
    cell.title.frame = CGRectMake(125, 5, titlerect.size.width, 28);
    cell.title.text = model.title;
    
    NSDictionary *dic15 = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    NSDictionary *dic1 = [model.array objectAtIndex:0];
    NSString *str1 = [NSString stringWithFormat:@"1.%@", [dic1 objectForKey:@"songName"]];
    CGRect reanrect1 = [str1 boundingRectWithSize:CGSizeMake(0, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic15 context:nil];
    cell.RankSingerName.text = str1;
    cell.RankSingerName.frame = CGRectMake(125, 40, reanrect1.size.width, 20);
    NSDictionary *dic2 = [model.array objectAtIndex:1];
    NSString *str2  = [NSString stringWithFormat:@"2.%@", [dic2 objectForKey:@"songName"]];
    CGRect reanrect2 = [str2 boundingRectWithSize:CGSizeMake(0, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic15 context:nil];
    cell.RankSingerName2.frame = CGRectMake(125, 60, reanrect2.size.width, 20);
    cell.RankSingerName2.text = str2;
    NSDictionary *dic3 = [model.array objectAtIndex:2];
    NSString *str3 = [NSString stringWithFormat:@"3.%@", [dic3 objectForKey:@"songName"]];
    CGRect reanrect3 = [str3 boundingRectWithSize:CGSizeMake(0, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic15 context:nil];
    cell.RankSingerName3.text = str3;
    cell.RankSingerName3.frame = CGRectMake(125, 80, reanrect3.size.width, 20);
    [cell.RankPhoto sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.pic_url]] placeholderImage:nil options:SDWebImageRetryFailed];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  110;
}
- (void)requestnet
{
    NSString *str = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=281&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.4&active=1&net=2&openudid=0494f4176d75510ad1a6542295634d790aad399e&idfa=F46187C9-E6C9-40A1-B881-A47F8EEEB191&utdid=VVtLXHT237sDAJf6mXsYmzAG&alf=201200&bundle_id=com.ttpod.music"];
    NSString *address = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manger GET:address parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSData *data = [NSData dataWithData:responseObject];
        NSDictionary *htmldic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArray = [htmldic objectForKey:@"data"];
        self.modelArray = [NSMutableArray array];
        for (NSDictionary *dic in dataArray) {
            SingerModel *model = [[[SingerModel alloc]init] autorelease];
            [model setValuesForKeysWithDictionary:dic];
            model.array = [dic objectForKey:@"songlist"];
            [self.modelArray addObject:model];
        }
        
        [self.mytableview reloadData];
        self.mytableview.separatorColor = [UIColor colorWithWhite:0.5 alpha:0.7];
        [self.pendulum removeFromSuperview];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    rankListController *rank = [[[rankListController alloc]init] autorelease];
    rank.myModel = [[SingerModel alloc]init];
    rank.myModel = [self.modelArray objectAtIndex:indexPath.row];
    SingerModel *model = [[[SingerModel alloc]init] autorelease];
    model = [self.modelArray objectAtIndex:indexPath.row];
    rank.address = model.songlist_id;
    NSLog(@"%@", [self.modelArray objectAtIndex:indexPath.row]);
    [self.navigationController pushViewController:rank animated:YES];
}



@end
